package com.gexiaobao.pigeon.ui.fragment.mine.organization.video

import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import com.flyjingfish.openimagefulllib.GSYVideoPlayer
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityLiveVideoPlayBinding
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.viewmodel.VideoViewModel
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/9/19 8:26
 * @Describe    : 播放直播视频
 */
open class ActivityLiveVideoPlay : BaseActivity<VideoViewModel, ActivityLiveVideoPlayBinding>() {

    private var orientationUtils: OrientationUtils? = null

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)

        val liveUrl = intent.getStringExtra("url").toString()
        mDatabind.videoPlayer.setUp(liveUrl, true, "")
        initPlayer()
    }

    private fun initPlayer() {
        //增加封面
        val imageView = ImageView(this)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        imageView.setImageResource(R.mipmap.app_icon_)
        mDatabind.videoPlayer.thumbImageView = imageView
        //增加title
        mDatabind.videoPlayer.titleTextView.visibility = View.VISIBLE
        //设置返回键
        mDatabind.videoPlayer.backButton.visibility = View.VISIBLE
        //设置旋转
        orientationUtils = OrientationUtils(this, mDatabind.videoPlayer)
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        mDatabind.videoPlayer.fullscreenButton.setOnClickListener { // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
            // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
            //orientationUtils.resolveByClick();
            finish()
        }
        //是否可以滑动调整
        mDatabind.videoPlayer.setIsTouchWiget(true)
        //设置返回按键功能
        mDatabind.videoPlayer.backButton.setOnClickListener { finish() }
        ///不需要屏幕旋转
        mDatabind.videoPlayer.isNeedOrientationUtils = false

        // 设置回调
        mDatabind.videoPlayer.setVideoAllCallBack(object : VideoAllCallBack {
            override fun onStartPrepared(url: String?, vararg objects: Any?) {}
            override fun onPrepared(url: String?, vararg objects: Any?) {}
            override fun onClickStartIcon(url: String?, vararg objects: Any?) {}
            override fun onClickStartError(url: String?, vararg objects: Any?) {}
            override fun onClickStop(url: String?, vararg objects: Any?) {}
            override fun onClickStopFullscreen(url: String?, vararg objects: Any?) {}
            override fun onClickResume(url: String?, vararg objects: Any?) {}
            override fun onClickResumeFullscreen(url: String?, vararg objects: Any?) {}
            override fun onClickSeekbar(url: String?, vararg objects: Any?) {}
            override fun onClickSeekbarFullscreen(url: String?, vararg objects: Any?) {}
            override fun onAutoComplete(url: String?, vararg objects: Any?) {}
            override fun onComplete(url: String?, vararg objects: Any?) {}
            override fun onEnterFullscreen(url: String?, vararg objects: Any?) {}
            override fun onQuitFullscreen(url: String?, vararg objects: Any?) {}
            override fun onQuitSmallWidget(url: String?, vararg objects: Any?) {}
            override fun onEnterSmallWidget(url: String?, vararg objects: Any?) {}
            override fun onTouchScreenSeekVolume(url: String?, vararg objects: Any?) {}
            override fun onTouchScreenSeekPosition(url: String?, vararg objects: Any?) {}
            override fun onTouchScreenSeekLight(url: String?, vararg objects: Any?) {}
            override fun onPlayError(url: String?, vararg objects: Any?) {
                showErrDialog()
            }

            override fun onClickStartThumb(url: String?, vararg objects: Any?) {}
            override fun onClickBlank(url: String?, vararg objects: Any?) {}
            override fun onClickBlankFullscreen(url: String?, vararg objects: Any?) {}
        })

        mDatabind.videoPlayer.startPlayLogic()
    }

    private fun showErrDialog() {
        val dialog = RxDialogDefault(this)
        dialog.setTitle(getString(R.string.video_play_fail))
        dialog.setRightBtnContent(getString(R.string.retry))
        dialog.setLeftBtnContent(getString(R.string.backing))
        dialog.setCancelListener {
            dialog.dismiss()
            finish()
        }
        dialog.setSureListener {
            mDatabind.videoPlayer.startPlayLogic()
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }


    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        // 检查当前方向并作相应处理
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏逻辑
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            // 竖屏逻辑
        }
    }

    override fun onPause() {
        super.onPause()
        mDatabind.videoPlayer.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        mDatabind.videoPlayer.onVideoResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
        if (orientationUtils != null) orientationUtils!!.releaseListener()
    }

}